package com.webinsky.article.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.article.persist.entity.ArticleBody;

public interface ArticleBodyMapper {
	
	@Delete(
			"delete from `article_body` where article_id = #{articleId, jdbcType=INTEGER}"
	)
	int deleteByArticleId(@Param("articleId")int articleId);
	
	 @Insert({
		"insert into article_body (article_id, index_id, content, type, sort, del, create_at, update_at, language, tips, tips_color)",
		"values (#{articleId,jdbcType=INTEGER}, #{indexId,jdbcType=INTEGER}, #{content,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER},  #{sort,jdbcType=INTEGER},",
		"#{del, jdbcType=INTEGER}, now(), now(), #{language,jdbcType=VARCHAR}, #{tips, jdbcType=VARCHAR}, #{tipsColor, jdbcType=VARCHAR})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(ArticleBody record);
	    
	@Select({
	    "select",
	    "id, article_id, index_id, content, type, sort, del, create_at, update_at, language, tips, tips_color",
	    "from article_body"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="index_id", property="indexId", jdbcType=JdbcType.INTEGER),
	    @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
	    @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="tips", property="tips", jdbcType=JdbcType.VARCHAR),
	    @Result(column="tips_color", property="tipsColor", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleBody> selectAll();
	
	@Select({
	    "select",
	    "id, article_id, index_id, content, type, sort, del, create_at, update_at, language, tips, tips_color",
	    "from article_body ",
	    "where id = #{id, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="index_id", property="indexId", jdbcType=JdbcType.INTEGER),
	    @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
	    @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="tips", property="tips", jdbcType=JdbcType.VARCHAR),
	    @Result(column="tips_color", property="tipsColor", jdbcType=JdbcType.VARCHAR)
	})	
	ArticleBody selectById(@Param("id")int id);
	
	@Select({
		"select",
	    "id, article_id, index_id, content, type, sort, del, create_at, update_at, language, tips, tips_color",
	    "from article_body ",
	    "where article_id = #{articleId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="index_id", property="indexId", jdbcType=JdbcType.INTEGER),
	    @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
	    @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	    @Result(column="tips", property="tips", jdbcType=JdbcType.VARCHAR),
	    @Result(column="tips_color", property="tipsColor", jdbcType=JdbcType.VARCHAR)
	})
	List<ArticleBody> selectByArticleIdAndDel(@Param("articleId")int articleId, @Param("del")int del);	
	
	@Update({
       "update article_body",
       "set article_id = #{articleId,jdbcType=INTEGER},",
       "tips = #{tips, jdbcType=VARCHAR},",
       "tips_color = #{tipsColor, jdbcType=VARCHAR},",
       "index_id = #{indexId,jdbcType=INTEGER},",
       "content = #{content,jdbcType=VARCHAR},",
       "type = #{type,jdbcType=INTEGER},",
       "sort = #{sort, jdbcType=INTEGER},",
       "del = #{del,jdbcType=INTEGER},",
       "create_at = #{createAt,jdbcType=TIMESTAMP},",
       "update_at = #{updateAt,jdbcType=TIMESTAMP},",
       "language = #{language,jdbcType=VARCHAR}",
       "where Id = #{id,jdbcType=INTEGER}"
   })
   int updateByPrimaryKey(ArticleBody record);	
}
